package user

import (
	"context"
	"strconv"

	"mall/common/utils/pointy"
	"mall/service/backend/api/internal/logic/system/util"
	"mall/service/backend/api/internal/logic/utils"
	"mall/service/backend/api/internal/logic/utils/parse"
	"mall/service/backend/api/internal/svc"
	"mall/service/backend/api/internal/types"
	"mall/service/backend/common/i18n"
	"mall/service/backend/rpc/backendclient"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetUserListLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewGetUserListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetUserListLogic {
	return &GetUserListLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *GetUserListLogic) GetUserList(req *types.SystemUserListReq) (resp *types.SystemUserListResp, err error) {
	page, pageSize := parse.ParsePageInfo(
		req.Current,
		req.PageSize,
		l.svcCtx.Config.Query.DefaultAmount,
		l.svcCtx.Config.Query.MaxAmount,
	)

	orderBys := util.ParseSorter(req.Sorter)
	filterBys := util.ParseFilter(req.Filter)

	var ids *string
	if req.Id != nil && *req.Id > 0 {
		ids = pointy.GetPointer(strconv.FormatInt(int64(*req.Id), 10))
	}
	var departmentId *uint64
	var positionId *uint64
	if len(req.Department) >= 2 {
		positionId = pointy.GetPointer(req.Department[1])
	}
	if len(req.Department) >= 1 {
		departmentId = pointy.GetPointer(req.Department[0])
	}

	data, err := l.svcCtx.BackendRpc.GetUserList(l.ctx, &backendclient.UserListReq{
		Page:         page,
		PageSize:     pageSize,
		Ids:          ids,
		Username:     req.Username,
		Nickname:     req.Nickname,
		RoleIds:      req.RoleIds,
		Status:       req.Status,
		Email:        req.Email,
		Mobile:       req.Mobile,
		DepartmentId: departmentId,
		PositionId:   positionId,
		StartTime:    req.StartTime,
		EndTime:      req.EndTime,
		OrderBy:      orderBys,
		FilterBy:     filterBys,
		WithRoles:    pointy.GetPointer(uint32(1)),
	})
	if err != nil {
		return
	}

	resp = &types.SystemUserListResp{
		BaseDataInfo: types.BaseDataInfo{
			Success: true,
			Msg:     l.svcCtx.Trans.Trans(l.ctx, i18n.Success),
		},
		Data: types.SystemUserListInfo{
			BaseListInfo: types.BaseListInfo{
				Current:  page,
				PageSize: pageSize,
				Total:    data.GetTotal(),
			},
		},
	}
	for _, v := range data.List {
		option := utils.GetOptionByStatusAndRoleIds(v.Status, v.RoleIds)
		resp.Data.List = append(resp.Data.List, &types.SystemUserInfo{
			BaseIDInfo: types.BaseIDInfo{
				Id:         v.Id,
				CreatedAt:  v.CreatedAt,
				UpdatedAt:  v.UpdatedAt,
				DeletedAt:  v.DeletedAt,
				CanAccept:  option.CanAccept,
				CanReject:  option.CanReject,
				CanRemove:  option.CanRemove,
				CanRestore: option.CanRestore,
			},
			Avatar:        v.Avatar,
			DepartmentId:  v.DepartmentId,
			Description:   v.Description,
			Email:         v.Email,
			HomePath:      v.HomePath,
			Mobile:        v.Mobile,
			Nickname:      v.Nickname,
			RoleIds:       v.RoleIds,
			Status:        v.Status,
			Username:      v.Username,
			LastLoginTime: v.LastLoginTime,
		})
	}

	return resp, nil
}
